package com.example.api.service;

import com.example.api.entity.UserEntity;
import com.example.api.feign.MemberServiceFeign;
import com.example.base.BaseApiService;
import com.example.base.ResponseBase;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Create with Intellij idea
 * User:鲁智明
 * Date:2018/11/17
 * Time:19:51
 */
@RestController
public class OrderServiceImpl extends BaseApiService implements OrderService {

    @Resource
    private MemberServiceFeign memberServiceFeign;

    @RequestMapping("/OrderToMember")
    public String OrderToMember(@RequestParam("name")String name) {
        System.out.println("OrderToMember:"+Thread.currentThread().getName());
        UserEntity userEntity=memberServiceFeign.getMember(name);
        return userEntity==null?"没有找到用户信息":userEntity.toString();
    }

    //没有解决服务雪崩效应
    @RequestMapping("/OrderToMemberUserInfo")
    public ResponseBase OrderToMemberUserInfo() {
        return memberServiceFeign.getUserInfo();
    }

    /**
     *  @HystrixCommand 默认开启服务隔离方式，以线程池隔离
     *                  默认开启服务降级方法fallbackMethod=OrderToMemberUserInfoHystrixFallBack
     *                  默认开启服务熔断机制
     *
     */

    //解决服务雪崩效应
    @RequestMapping("/OrderToMemberUserInfohystrix")
    @HystrixCommand(fallbackMethod = "OrderToMemberUserInfoHystrixFallBack")
    public ResponseBase OrderToMemberUserInfoHystrix() {
        System.out.println("OrderToMemberUserInfoHystrixFallBack"+"线程池名称:"+Thread.currentThread().getName());
        // 默认情况下，如果1秒之内没有响应给客户端请求结果，直接走服务降级方法。业务逻辑会执行但是如果1秒之内没有响应给客户端结果，就会走服务降级方法。
        return memberServiceFeign.getUserInfo();
    }
    //服务降级方法
    public ResponseBase OrderToMemberUserInfoHystrixFallBack() {
        return setResultSuccess("亲，当前服务器忙，请稍候再试!");
    }

    // Hystrix 第二种方式  通过类实现
    @RequestMapping("/OrderToMemberUserInfohystrix02")
    public ResponseBase OrderToMemberUserInfohystrix02() {
        System.out.println("OrderToMemberUserInfoHystrixFallBack"+"线程池名称:"+Thread.currentThread().getName());
        return memberServiceFeign.getUserInfo();
    }


    //订单服务接口
    @RequestMapping("/orderInfo")
    public ResponseBase orderInfo(){
        System.out.println("orderInfo"+"线程池名称:"+Thread.currentThread().getName());
        return setResultSuccess();
    }

    //hystrix 有2中方式配置服务保护 通过注解方式和接口方式
}
